<?php


namespace app\admin\controller;


use app\admin\model\AfterServiceModel;
use app\admin\model\RoleUserModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;

class AfterSaleServicesController extends AdminBaseController
{
    public function index()
    {
        $admin_id = cmf_get_current_admin_id();
        $role_id = RoleUserModel::where('user_id', $admin_id)->value('role_id');
        $role_id = $role_id ? $role_id : 1;
        $this->assign('role_id', $role_id);
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $num = 10;
            $page = isset($data['page']) && $data['page'] ? intval($data['page']) : 1;
            $where = [];
            $admin_id = cmf_get_current_admin_id();
            $admin_info = UserModel::get($admin_id);
            if (isset($data['duration']) && $data['duration']) {
                $where[] = ['create_time', 'between time', [$data['duration'][0], $data['duration'][1]]];
            }
            if ($admin_info['company_id']) {
                $where[] = ['company_id', '=', $admin_info['company_id']];
            } else {
                $company_id = isset($data['company_id']) && $data['company_id'] ? $data['company_id'] : 0;
                if ($company_id) {
                    $where[] = ['company_id', '=', $company_id];
                }
            }
            $list = AfterServiceModel::with('user_info,company_info')->where($where)->order('create_time desc')
                ->paginate($num, false, ['page' => $page]);
            $this->success('', '', $list);
        }
        return $this->fetch();
    }

    public function handle_services()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $info = AfterServiceModel::get($data['id']);
            if (empty($info)) {
                $this->error('信息不存在');
            }
            $info->result = $data['result'];
            $info->status = 1;
            $res = $info->save();
            if ($res) {
                $this->success('处理成功');
            } else {
                $this->error('处理失败');
            }
        }
    }
}
